package com.bhs.sansonglogistics.ui.other;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.DataBoardPlatform;
import com.bhs.sansonglogistics.bean.InformationBean;
import com.bhs.sansonglogistics.bean.event.ViewPageStatus;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据看板底部数据
 */
public class DataBoardPlatformListFragment extends BaseFragment {

    private RecyclerView mRvList;
    private BaseQuickAdapter<InformationBean, BaseViewHolder> mAdapter;
    private int type;

    public DataBoardPlatformListFragment() {

    }

    public static DataBoardPlatformListFragment newInstance(int type) {
        DataBoardPlatformListFragment fragment = new DataBoardPlatformListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public int initLayout() {
        return R.layout.fragment_data_board_platform_list;
    }

    @Override
    public void initView(View root) {
        type = getArguments().getInt("type");
        mRvList = root.findViewById(R.id.rv_list);
    }

    @Override
    public void initData() {
        mAdapter = new BaseQuickAdapter<InformationBean, BaseViewHolder>(R.layout.item_information) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, InformationBean item) {
                Glide.with(getContext()).load(item.getFace()).into((ImageView) helper.getView(R.id.iv_face));
                helper.setText(R.id.tv_nick_name, item.getName());
                helper.setText(R.id.tv_phone, MyUtils.phoneHide(item.getPhone()));
                helper.setGone(R.id.tv_phone, type == 1);
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        EventBus.getDefault().register(this);
    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(DataBoardPlatform event) {
        List<InformationBean> list = new ArrayList<>();
        if (type == 1) {
            for (DataBoardPlatform.DataDTO.ActiveListDTO activeListDTO : event.getData().getActive_list()) {
                list.add(new InformationBean(activeListDTO.getHeaderpic(), activeListDTO.getNickname(), activeListDTO.getMobile()));
            }
        } else {
            for (DataBoardPlatform.DataDTO.LogisticsListDTO lineListDTO : event.getData().getLogistics_list()) {
                if (TextUtils.isEmpty(lineListDTO.getCompany_pic())) {
                    list.add(new InformationBean("", lineListDTO.getCompany_name(), ""));
                } else {
                    list.add(new InformationBean(lineListDTO.getCompany_pic(), lineListDTO.getCompany_name(), ""));
                }
            }
        }
        mAdapter.setNewData(list);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}